var ejsexcel = require("ejsexcel");
var fs = require("fs");
var path = require("path");
var filePath = "";
var exlBuf = fs.readFileSync("./template2.xlsx");
var cpsActivelistDao = require('../dao/cps_activelist_dao');
var cpsSignuplistDao = require('../dao/cps_signuplist_dao');
var cps_activelist_model = require('../model/cps_activelist_model');

// cpsActivelistDao.update(function (err, data) {}, 3, {
//     ext1: Math.round(new Date() / 1000)
// });
// 招商活动列表
// cpsActivelistDao.queryActivelist(function (err, data) {
//     var _t = [];
//     try{
//         data.data.forEach(function (ceil) {
//             var info = JSON.parse(ceil.info);
//             _t.push({
//                 active_id: ceil.active_id,
//                 active_title: ceil.active_title,
//                 user_pin: ceil.user_pin,
//                 user_nick: ceil.user_nick,
//                 end_time: ceil.end_time,
//                 time: ceil.start_time + " - " + ceil.end_time,
//                 phone_num: ceil.phone_num,
//                 dsr: info.dsr,
//                 ecount: info.ecount,
//                 gecount: info.gecount,
//                 pricelimit: (info.pricestart || info.prisestart) + " - " + (info.priceend || info.priseend),
//                 is30low: info.is30low ? "30天最低" : "",
//                 freesend: info.freesend ? "全国包邮" : "",
//                 cateinfo: info.cateinfo,
//                 hascoupon: info.hascoupon ? "要求有优惠券" : "",
//                 expect_count: ceil.expect_count,
//                 createtime: ceil.ext1 ? $formatDate(new Date(ceil.ext1 * 1000), "YYYY-MM-DD HH:II:SS") : "",
//                 signed_num: ceil.signed_num
//             });
//         });
//         ejsexcel.renderExcel(exlBuf, _t).then(function (data) {
//             fs.writeFileSync("./output.xlsx", data);
//             console.log("写入成功");    
//         }).catch(function (e) {
//             console.log("写入失败", e);
//         });
//     }catch(e){console.log(e);}
//     // console.log(err, data);
// }, 1, 10000, 3);

// 报名信息表
// cpsSignuplistDao.querySignup(function (err, data) {
//     var _t = [];
//     try{
//         data.data.forEach(function (ceil) {
//             ceil.skuinfos = JSON.parse(ceil.skuinfos);
//             ceil.createtime = $formatDate(new Date(ceil.ext1 * 1000), "YYYY-MM-DD HH:II:SS");
//         });
//         ejsexcel.renderExcel(exlBuf, data.data).then(function (data) {
//             fs.writeFileSync("./output.xlsx", data);
//             console.log("写入成功");    
//         }).catch(function (e) {
//             console.log("写入失败", e);
//         });
//     }catch(e){console.log(e);}
//     // console.log(err, data);
// }, 3, 1, 10000);

// console.log("__dirname = %s", path.resolve(__dirname));
// console.log("pwd = %s", process.cwd());

cps_activelist_model.getDownloadActiveFile(function (err, data) {
    console.log(err, data);
});

// cpsSignuplistDao.querySignup(function (err, data) {
//     console.log(err, data);
// }, 3, 1, 10000);

function $formatDate(date,formatStr){   
    //格式化时间  
    var arrWeek=['日','一','二','三','四','五','六'],  
        str=formatStr  
            .replace(/yyyy|YYYY/,date.getFullYear())  
            .replace(/yy|YY/,$addZero(date.getFullYear(),2))  
            .replace(/mm|MM/,$addZero(date.getMonth()+1,2))  
            .replace(/m|M/g,date.getMonth()+1)  
            .replace(/dd|DD/,$addZero(date.getDate(),2))  
            .replace(/d|D/g,date.getDate())  
            .replace(/hh|HH/,$addZero(date.getHours(),2))  
            .replace(/h|H/g,date.getHours())  
            .replace(/ii|II/,$addZero(date.getMinutes(),2))  
            .replace(/i|I/g,date.getMinutes())  
            .replace(/ss|SS/,$addZero(date.getSeconds(),2))  
            .replace(/s|S/g,date.getSeconds())  
            .replace(/w/g,date.getDay())  
            .replace(/W/g,arrWeek[date.getDay()]);   
    return str;   
}  
function $addZero(v,size){  
    for(var i=0,len=size-(v+"").length;i<len;i++){  
        v="0"+v;  
    };  
    return v+"";  
} 